#pragma once
#include "Student.h"
#include <sstream>
#include <vector>
#include <fstream>

class StudentRegister
{
private:
	vector<Student> v;
public:
	StudentRegister(){}
	~StudentRegister(){}
	
	void Add(Student a)
	{
		v.push_back(a);
	}

	string List()
	{
		ostringstream oss;
		for(int i = 0; i < v.size(); i++)
		{
			oss << v[i] << endl;
		}
		return oss.str();
	}

	void WriteToFile(string filename)
	{
		ofstream fout;
		fout.open(filename);
		for(auto &a : v)
		{
			fout << "Student=" << a << endl;
		}
	fout.close();
	}

	void ReadFromFile(string filename)
	{
		string str, strRow;
		ifstream fin;
		fin.open(filename);
		if(!fin.good())
		{
			cout << "Could not open file!" << endl;
			return;
		}
		v.clear();
		while(std::getline(fin, strRow))
		{
			istringstream iss(strRow);
			getline(iss, str, '=');
			if(str.compare("Student") == 0)
			{
				Student a;
				iss >> a;
				cout << a << endl;;
			}

		}
		fin.close();
	}
};

void main3_1_2()
{
	cout << "main3_1_2" << endl;
	Student a;
	a.SetfirstName("Ola");
	a.SetsecondName("Bertsson");
	a.SetenrollmentYear(2);
	cout << "Firstname= " << a.GetfirstName() << endl;
	cout << "Secondname= " << a.GetsecondName() << endl;
	cout << "Enrollment year= " << a.GetenrollmentYear() << endl;
	a.Read();
	a.Write();
}

void main3_3()
{
	cout << "main3_3" << endl;
	Student b;
	cin >> b;
	cout << b << endl;
}

void main3_4()
{
	cout << "main3_4" << endl;
	StudentRegister a;
	Student b,c;
	b.Read();
	c.Read();
	a.Add(b);
	a.Add(c);
	cout << a.List();

}

void main3_5()
{
	cout << "main3_5" << endl;
	StudentRegister a;
	Student b, c, d;
	b.Read();
	c.Read();
	d.Read();
	a.Add(b);
	a.Add(c);
	a.Add(d);
	a.WriteToFile("tja.txt");
}

void main3_6()
{
	cout << "main3_6" << endl;
	StudentRegister a, e;
	Student b, c, d;
	b.Read();
	c.Read();
	d.Read();
	a.Add(b);
	a.Add(c);
	a.Add(d);
	a.WriteToFile("hej.txt");
	e.ReadFromFile("hej.txt");
}


int main()
{
	main3_1_2();
	main3_3();
	main3_4();
	main3_5();
	main3_6();
	cin.get();
	cin.get();
}